﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.baidu.mobstat.StatService;
import com.thorqq.magictimer.core.TTimerLog;
import com.thorqq.magictimer.db.DBHelper;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class TimerLogActivity extends Activity
{

    private ListView mTimerLogListView;
    private AdapterTimerLog mTimerLogAdapter;
    private Vector<TTimerLog> mTimerLogArray;
    private ArrayList<Map<String, Object>> mTimerLogItemList = new ArrayList<Map<String, Object>>();

    private ProgressBar mProgress;
    private Button mBtnClear;
    private Button mBtnReturn;

    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.timerlog_query);

        mTimerLogListView = (ListView) findViewById(R.id.listViewTimerLog);
        mTimerLogAdapter = new AdapterTimerLog(this, mTimerLogItemList);
        mTimerLogListView.setAdapter(mTimerLogAdapter);
        mTimerLogListView.setEmptyView(findViewById(R.id.tvNoTimerLog));
        
        mProgress = (ProgressBar) findViewById(R.id.progressBarQueryTimerLog);
        mBtnClear = (Button) findViewById(R.id.btn_log_clear);
        mBtnReturn = (Button) findViewById(R.id.btn_log_return);

        registerListener();
        
        updateLayout();
    }
    
    public void registerListener()
    {
        //清空
        mBtnClear.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                DBHelper.getIntance().deleteTimerLog();
                mTimerLogItemList.clear();
                mTimerLogAdapter.notifyDataSetChanged();
            }
        });
        
        //返回
        mBtnReturn.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                finish();
            }
        });
    }
    
    public void updateLayout()
    {
        new AsyncTask<Void, Void, Boolean>()
        {
            @Override
            protected void onPreExecute()
            {
                mProgress.setVisibility(View.VISIBLE);
                super.onPreExecute();
            }

            @Override
            protected Boolean doInBackground(Void... params) 
            {
                mTimerLogArray = DBHelper.getIntance().queryTimerLog();
                
                return true;
            }
            
            @Override
            protected void onProgressUpdate(Void... b )
            {
            }

            @Override
            protected void onPostExecute(Boolean result) 
            {
                mTimerLogItemList.clear();
                for(TTimerLog timerLog : mTimerLogArray)
                {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("timerLog", timerLog);
                    mTimerLogItemList.add(map);
                }
                
                mTimerLogAdapter.notifyDataSetChanged();

                mProgress.setVisibility(View.GONE);
            }
        }.execute();
        
    }

    @Override
    public void onResume()
    {
        super.onResume();

        /**
         * 此处调用基本统计代码
         */
        StatService.onResume(this);
    }

    @Override
    public void onPause()
    {
        super.onPause();

        /**
         * 此处调用基本统计代码
         */
        StatService.onPause(this);
    }

}
